home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / pasclern.zip / CHAP5.TXT < prev    next >
Text File  |  1993-04-01  |  22KB  |  455 lines

  1.              CHAPTER 5 - The Pascal procedures and functions
  2.  
  3.  
  4.             In order to define the procedures and functions we  will 
  5.         need   to  lay  some  groundwork  in  the  form  of  a   few 
  6.         definitions.   These  are important concepts,  so pay  close 
  7.         attention.
  8.  
  9.         Program  Heading - This is the easiest part since it is only 
  10.                    one  line,  at  least it has been in all  of  our 
  11.                    programs  up  to  this point.  It is  simply  the 
  12.                    PROGRAM line,  and it never needs to be any  more 
  13.                    involved  than  it has been up to this  point  in 
  14.                    TURBO Pascal.
  15.  
  16.         Declaration  Part  - This is the part of the  Pascal  source 
  17.                    code in which all constants,  variables, and user 
  18.                    defined  auxiliary  operations  are  defined.  In 
  19.                    some of the programs we have examined, there have 
  20.                    been one or more VAR declarations.  These are the 
  21.                    only  components of the declaration part we  have 
  22.                    used  to  this point.  There  are  actually  five 
  23.                    components  in  the  declaration  part,  and  the 
  24.                    procedures  and functions are the fifth part.  We 
  25.                    will cover the others in the next chapter.
  26.  
  27.         Statement  Part  - This  is  the last  part  of  any  Pascal 
  28.                    program,  and it is what we have been calling the 
  29.                    main  program.   It  is  one  compound  statement 
  30.                    bracketed with the reserved words BEGIN and END.
  31.  
  32.             It   is   very  important  that  you  grasp  the   above 
  33.         definitions because we will be referring to them  constantly 
  34.         during  this chapter,  and throughout the remainder of  this 
  35.         tutorial.   With that introduction,  lets go on to our first 
  36.         Pascal program with a procedure in it, in fact, it will have 
  37.         three procedures.
  38.  
  39.                            THE FIRST PROCEDURES
  40.  
  41.             Load  PROCED1  as your first working file and  edit  it.  
  42.         You  will notice that it doesn't look like anything you have 
  43.         seen up to this point because it has PROCEDUREs in it.  Lets 
  44.         go  back to our definitions from above.   The first line  is 
  45.         the  Program Heading which should pose no  difficulty.   The 
  46.         Declaration Part begins with the VAR statement and continues 
  47.         down through and including all three procedures.   The  last 
  48.         five  lines  constitute  the Statement Part.   It  may  seem 
  49.         strange   that   what  appears  to  be   executable   Pascal 
  50.         statements, and indeed they are executable, are contained in 
  51.         the Declaration Part rather than the Statement  Part.   This 
  52.         is  because of the Pascal definition and it will make  sense 
  53.         when  we  have  completed our study of  the  procedures  and 
  54.         functions.
  55.  
  56.  
  57.                                  Page 20
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.              CHAPTER 5 - The Pascal procedures and functions
  68.  
  69.  
  70.  
  71.             Continuing to examine PROCED1,  we will make note of the 
  72.         program itself,  which is the Statement Part.   The program, 
  73.         due  to the nature of Pascal,  clearly tells us what it will 
  74.         do,  namely write a header,  eight messages,  and an ending.  
  75.         The  only problem we are faced with is,  how will  it  write 
  76.         these messages? This is where the Declaration Part is called 
  77.         upon to define these operations in detail.   The Declaration 
  78.         Part  contains three procedures which will completely define 
  79.         what  is to be done in the main program,  or  the  Statement 
  80.         Part.  Hopefully you can begin to see why the procedures are 
  81.         included in the Declaration Part of the Pascal program.
  82.  
  83.             Now  to  examine  one procedure in  detail,  namely  the 
  84.         first.   The first statement we come to in the main  program 
  85.         is the one that says simply,  "write_a_header" followed with 
  86.         the usual end of statement, the semicolon.  This is a simple 
  87.         procedure  call.   When  the  compiler finds  this  it  goes 
  88.         looking  for a predefined procedure which it can execute  at 
  89.         this point.   If it finds one in the Declaration Part of the 
  90.         program, it will execute that procedure.  If it doesn't find 
  91.         a user defined procedure,  it will search the Pascal library 
  92.         for  a system defined procedure and execute it.   The  WRITE 
  93.         and   WRITELN   statements  are  actually   system   defined 
  94.         procedures,  and  you  have already been using them quite  a 
  95.         bit,  so  procedures are not completely new to you.   If  it 
  96.         doesn't find the procedure defined in either place,  it will 
  97.         generate an error message.
  98.  
  99.                           HOW TO CALL A PROCEDURE
  100.  
  101.             To call a procedure,  we simply need to state its  name.  
  102.         To  define  a  simple procedure,  we use the  reserved  word 
  103.         PROCEDURE followed by its calling name,  with a semicolon as 
  104.         a terminator.  Following the Procedure Heading, there is the 
  105.         Declaration  Part of the procedure followed by a body  which 
  106.         is  nothing more than a compound statement bracketed by  the 
  107.         reserved  words  BEGIN and END.   This is identical  to  the 
  108.         Statement Part of the main program except that the procedure 
  109.         ends with a semicolon instead of a period.  Any valid Pascal 
  110.         statements  can  be put between the BEGIN and  END,  and  in 
  111.         fact,  there  is  no  difference in what can  be  put  in  a 
  112.         procedure and what can be put in the main program.
  113.  
  114.             The program we are examining would be no different if we 
  115.         would  eliminate the first procedure completely and move the 
  116.         WRITELN contained in it down to the Statement Part in  place 
  117.         of  "write_a_header".   If  that is not clear,  go back  and 
  118.         reread these last two paragraphs until it is.
  119.  
  120.  
  121.  
  122.  
  123.                                  Page 21
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.              CHAPTER 5 - The Pascal procedures and functions
  134.  
  135.  
  136.             The   next   line  will  cause   the   procedure   named 
  137.         "write_a_message" to be called 8 times,  each time writing a 
  138.         line  of  output.   Suffice it to say at this time that  the 
  139.         value of "count",  as defined here, is available "globally", 
  140.         meaning  anywhere  in the entire Pascal  program.   We  will 
  141.         define the variable availability shortly.  Finally, the last 
  142.         procedure  call is made,  causing the ending message  to  be 
  143.         displayed,  and  the program execution is complete.   Having 
  144.         examined your first Pascal procedures, there is a fine point 
  145.         that  is  obvious  but  could  be  easily  overlooked.    We 
  146.         mentioned the unbroken rule of Pascal in an earlier  chapter 
  147.         and  it must be followed here too.   "Nothing can be used in 
  148.         Pascal until it has been defined".   The procedures must all 
  149.         be  defined  ahead  of  any  calls  to  them,   once   again 
  150.         emphasizing  the fact that they are part of the  Declaration 
  151.         Part of the program, not the Statement Part.
  152.  
  153.                            MORE PROCEDURE CALLS
  154.  
  155.             Assuming   you   have  run  PROCED1   successfully   and 
  156.         understand its output, lets go on to PROCED2 and examine it.  
  157.         To  begin with,  notice that there are three procedure calls 
  158.         in  the  Statement  Part  of the program  and  each  has  an 
  159.         additional  term  not  contained in the calls  in  the  last 
  160.         program,  namely the word "index" within brackets.   This is 
  161.         Pascals way of taking a variable parameter to the  procedure 
  162.         when  it  is  called.   You will notice  that  the  variable 
  163.         "index" is defined as an integer variable in the very top of 
  164.         the    Declaration    Part.     Therefore   the    procedure 
  165.         "print_data_out" had better be expecting an integer variable 
  166.         or  we will have a type mismatch.   In fact,  observing  the 
  167.         procedure itself,  indicates that it is indeed expecting  an 
  168.         integer  variable  but  it  prefers  to  call  the  variable 
  169.         "puppy".  Calling it something different poses no problem as 
  170.         long  as  the main program doesn't try to call its  variable 
  171.         "puppy",  and  the  procedure doesn't try to  use  the  name 
  172.         "index".   Both  are actually referring to the same piece of 
  173.         data but they simply wish to refer to it by different names.  
  174.         Notice  that  the next procedure is called with index  as  a 
  175.         parameter  and  it  prefers to call it by  the  name  "cat".  
  176.         Notice that in both cases,  the procedures simply print  out 
  177.         the parameter passed to it,  and each then try to modify the 
  178.         value passed to it before passing it back.  We will see that 
  179.         one will be successful and the other will not.
  180.  
  181.             We  are in a loop in which "count" is incremented from 1 
  182.         to  3 and we are not allowed to modify the loop variable  so 
  183.         we  make a copy of the value and call it  "index".   We  can 
  184.         then modify "index" if we desire.   The first procedure does 
  185.         not  contain  a  VAR in front of the  passed  parameter  and 
  186.         therefore the parameter passing is only one way.  The system 
  187.  
  188.  
  189.                                  Page 22
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.              CHAPTER 5 - The Pascal procedures and functions
  200.  
  201.  
  202.         makes  a  copy  of  "index",  and passes  the  copy  to  the 
  203.         procedure which can do anything with it, of course using its 
  204.         new  name,  "puppy",  but  when control returns to the  main 
  205.         program,  the original value of "index" is still there.   So 
  206.         think  of  the passed parameter without the VAR as  one  way 
  207.         parameter passing.   This is a "call by value" because  only 
  208.         the value of the variable is passed to the procedure.
  209.  
  210.             The  second procedure has the reserved word VAR in front 
  211.         of its desired name for the variable,  namely "cat",  so  it 
  212.         can  not only receive the variable,  it can modify  it,  and 
  213.         return the modified value to the main program. A copy is not 
  214.         made.   The  original  variable  named "index"  is  actually 
  215.         passed  to this procedure and it can  modify  it,  therefore 
  216.         communicating to the main program.   A passed parameter with 
  217.         a VAR in front of it is therefore a two way situation.  This 
  218.         is a "call by reference" since the reference to the original 
  219.         variable is passed to the procedure.
  220.  
  221.             When you run this program,  you will find that the first 
  222.         procedure  is unable to get the value of 12 back to the main 
  223.         program,  but  the second procedure does in fact succeed  in 
  224.         returning  its value of 35 to the main  program.   Spend  as 
  225.         much  time as you like studying this program until you fully 
  226.         understand it.   It should be noted that as many  parameters 
  227.         as  desired can be passed to and from a procedure by  simply 
  228.         making  a  list  separated  by  commas  in  the  calls,  and 
  229.         separated by semicolons in the procedure.
  230.  
  231.             For  your  own  enlightenment,  examine PROCED3  for  an 
  232.         example  of a procedure call with more than one variable  in 
  233.         the call.   Normally, you would group the three input values 
  234.         together,   but  for  purposes  of  illustration,  they  are 
  235.         separated.   Observe that the variable "fruit" is a two  way 
  236.         variable.    When   you  are  satisfied  with  the   present 
  237.         illustration,  we will go on to study the scope of variables 
  238.         using PROCED4.
  239.  
  240.                         A MULTIPLY DEFINED VARIABLE
  241.  
  242.             If  you will examine PROCED4,  you will notice that  the 
  243.         variable "count" is defined twice,  once in the main program 
  244.         VAR  block  and once in the VAR block contained  within  the 
  245.         procedure named "print_some_data".   This is perfectly legal 
  246.         and is within the Pascal definition.
  247.  
  248.             The  variable "index" is defined in the main program VAR 
  249.         block  and  is  valid  anywhere  within  the  entire  Pascal 
  250.         program,  including the procedures.  The variable "count" is 
  251.         also defined in the main program VAR block and is also valid 
  252.         anywhere within the entire Pascal program, except within the 
  253.  
  254.  
  255.                                  Page 23
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.              CHAPTER 5 - The Pascal procedures and functions
  266.  
  267.  
  268.         procedure  where another variable is defined with  the  same 
  269.         name  "count".   The two variables with the same name are in 
  270.         fact,  two  completely  different  variables.   If  a  third 
  271.         variable  was  defined within the  procedure,  it  would  be 
  272.         invisible  to  the main program,  since it was defined at  a 
  273.         lower  level than that of the main  program.   Any  variable 
  274.         defined in the main program or in any procedure is available 
  275.         in  any  procedure that is within the scope of the  defining 
  276.         procedure.  That last sentence seems confusing, but thinking 
  277.         about it will clear it up.   This is a difficult concept  to 
  278.         grasp but is very important.
  279.  
  280.  
  281.                     PROCEDURES CALLING OTHER PROCEDURES
  282.  
  283.             Load and examine PROCED5 to see an example of procedures 
  284.         that call other procedures.  Keep in mind that, "Nothing can 
  285.         be used in Pascal until it has been previously defined", and 
  286.         the order of procedures will be clear in this example.
  287.  
  288.             Now that you have a good grasp of procedures, we need to 
  289.         make  another  important point.   Remember that  any  Pascal 
  290.         program is made up of three parts,  the Program Heading, the 
  291.         Declaration Part,  and the Statement Part.   The Declaration 
  292.         Part is composed of five unique components, four of which we 
  293.         will  discuss  in detail in the next chapter,  and the  last 
  294.         component,  which  is composed of some number of  procedures 
  295.         and functions.  We will cover functions in the next example, 
  296.         so  for  now  simply  accept the fact  that  it  is  like  a 
  297.         procedure.   A procedure is also composed of three parts,  a 
  298.         Program  Heading  (which says  "PROCEDURE"),  a  Declaration 
  299.         Part,  and a Statement Part.  A procedure, by definition, is 
  300.         therefore  nothing more or less than another complete Pascal 
  301.         program embedded within the main program,  and any number of 
  302.         procedures  can  be located in the Declaration Part  of  the 
  303.         main program.  These procedures are all in a line, one right 
  304.         after another.
  305.  
  306.             Since a procedure is defined like the main  program,  it 
  307.         would  seem to be possible to embed another procedure within 
  308.         the  declaration part of any procedure.   This is  perfectly 
  309.         valid  and  is often done,  but remember that  the  embedded 
  310.         procedure can only be called by the procedure in which it is 
  311.         embedded,  not by the main program.   That is probably a bit 
  312.         difficult to grasp.   Don't worry about it too much now,  as 
  313.         you become proficient as a Pascal programmer,  you will very 
  314.         clearly see how that is used.
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                                  Page 24
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.              CHAPTER 5 - The Pascal procedures and functions
  332.  
  333.  
  334.                        NOW LET'S LOOK AT A FUNCTION
  335.  
  336.             Now to keep a promise, lets examine FUNCTION to see what 
  337.         a  function  is  and how to use it.   In  this  very  simple 
  338.         program,  we have a function that simply multiplies the  sum 
  339.         of  two  variables by 4 and returns the result.   The  major 
  340.         difference  between a function and a procedure is  that  the 
  341.         function  returns a single value and is called from within a 
  342.         mathematical expression, a WRITELN command, or anywhere that 
  343.         it  is  valid  to put any variable,  since it  is  really  a 
  344.         variable  itself.   Observing  the Program  Heading  of  the 
  345.         function   reveals  the  two  input  variables  inside   the 
  346.         parenthesis  pair  being defined as INTEGER  variables,  and 
  347.         following  the parenthesis is a colon and  another  INTEGER.  
  348.         The  last INTEGER is used to define the type of the variable 
  349.         being returned to the main program.  Since any call to  this 
  350.         function  is actually replaced by an integer upon completion 
  351.         of the call,  it can be used anywhere in a program where  an 
  352.         integer variable can be used.
  353.  
  354.                      NOW FOR THE MYSTERY OF RECURSION
  355.  
  356.             One of the great mysteries of Pascal to many people,  is 
  357.         the recursion of procedure calls.  Simply defined, recursion 
  358.         is  the ability of a procedure to call itself.   Examine the 
  359.         Pascal  example file RECURSON for an example  of  recursion.  
  360.         The  main  program  is very simple,  it  sets  the  variable 
  361.         "count"   to   the   value  7  and   calls   the   procedure 
  362.         "print_and_decrement".   The  procedure prefers to refer  to 
  363.         the  variable by the name "index" but that poses no problem.  
  364.         The  procedure writes a line to the video display  with  the 
  365.         value of "index" written within the line, and decrements the 
  366.         variable.
  367.  
  368.             The IF statement introduces the interesting part of this 
  369.         program.   If  the variable is greater than zero,  and it is 
  370.         now  6,  then the procedure "print_and_decrement" is  called 
  371.         once again.   This might seem to create a problem except for 
  372.         the  fact  that this is perfectly  legal  in  Pascal.   Upon 
  373.         entering the procedure the second time, the value of "index" 
  374.         is printed as 6, and it is once again decremented.  Since it 
  375.         is  now 5,  the same procedure will be called again,  and it 
  376.         will continue until the value of "index" is reduced to  zero 
  377.         when each procedure call will be completed one at a time and 
  378.         control will return to the main program.
  379.  
  380.                         ABOUT RECURSIVE PROCEDURES
  381.  
  382.             This is really a stupid way to implement this particular 
  383.         program,  but  it is the simplest recursive program that can 
  384.         be  written and therefore the easiest  to  understand.   You 
  385.  
  386.  
  387.                                  Page 25
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.              CHAPTER 5 - The Pascal procedures and functions
  398.  
  399.  
  400.         will have occasional use for recursive procedures,  so don't 
  401.         be  afraid  to  try  them.    Remember  that  the  recursive 
  402.         procedure  must have some variable converging to  something, 
  403.         or you will have an infinite loop.
  404.  
  405.                            THE FORWARD REFERENCE
  406.  
  407.             Occasionally  you  will  have  a need  to  refer  to   a 
  408.         procedure  before you can define it.   In that case you will 
  409.         need  a  FORWARD  reference.   The program  FORWARD  has  an 
  410.         example of a forward reference in it.  In this program, each 
  411.         one of the procedures calls the other,  a form of recursion.  
  412.         This program,  like the last,  is a very stupid way to count 
  413.         from  7 to 0,  but it is the simplest program possible  with 
  414.         the forward reference.
  415.  
  416.             The  first procedure,  "write_a_line",  has  its  header 
  417.         defined  in  exactly the same manner as any other  procedure 
  418.         but instead of the normal procedure body,  only the reserved 
  419.         word  FORWARD is given.   This tells the compiler  that  the 
  420.         procedure  will  be defined later.   The next  procedure  is 
  421.         defined  as usual,  then the body of "write_a_line" is given 
  422.         with  only  the reserved word PROCEDURE  and  the  procedure 
  423.         name.   The variable reference has been defined earlier.  In 
  424.         this  way,  each  of the procedure names are defined  before 
  425.         they are called.
  426.  
  427.             It would be possible,  by using the FORWARD reference in 
  428.         great  numbers,  to  move  the main  program  ahead  of  all 
  429.         procedure  definitions and have the program structured  like 
  430.         some  other languages.   This style of programming would  be 
  431.         perfectly legal as far as the compiler is concerned, but the 
  432.         resulting  program would be very nonstandard and  confusing.  
  433.         You   would  do  well  to  stick  with  conventional  Pascal 
  434.         formatting   techniques  and  use  the   FORWARD   reference 
  435.         sparingly.
  436.  
  437.                            PROGRAMMING EXERCISES
  438.  
  439.         1.  Write a program to write your name,  address,  and phone 
  440.             number with each WRITELN in a different procedure.
  441.  
  442.         2.  Add a statement to the procedure in RECURSON to  display 
  443.             the value of "index" after the call to itself so you can 
  444.             see  the  value increasing as the  recurring  calls  are 
  445.             returned to the next higher level.
  446.  
  447.         3.  Rewrite  TEMPCONV putting the  centigrade to  fahrenheit 
  448.             formulas in a function call.
  449.  
  450.  
  451.  
  452.  
  453.                                  Page 26
  454.  
  455.